<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="公告标题" prop="noticeTitle">
        <el-input
          v-model="queryParams.info"
          placeholder="请输入公告标题"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="操作人员" prop="createBy">
        <el-input
          v-model="queryParams.createBy"
          placeholder="请输入操作人员"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="类型" prop="status">
        <el-select v-model="queryParams.status" placeholder="公告类型" clearable size="small">
          <el-option
            v-for="dict in typeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <!-- <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button> -->
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:notice:edit']"
        >修改</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="delAllSelection"
        >删除</el-button>
      </el-col>
    </el-row>

    <el-table  :data="noticeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        label="公告标题"
        align="center"
        prop="title"
      />
      <!-- <el-table-column label="公告类型" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.level==0">通知</span>
          <span v-else>公告</span>
        </template>
      </el-table-column> -->
    
      <!-- <el-table-column
        label="状态"
        align="center"
        prop="level"
        :formatter="statusFormat"
        width="100"
      /> -->
      <!-- <el-table-column label="创建者" align="center" prop="createBy" width="150" /> -->
      <el-table-column label="创建时间" align="center" prop="createTime">
        <template slot-scope="scope">
          <span>{{scope.row.createTime | formatDate()}}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改公告对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="780px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="公告标题" prop="noticeTitle">
              <el-input v-model="form.title" placeholder="请输入公告标题" />
            </el-form-item>
          </el-col>
          <!-- <el-col :span="12">
            <el-form-item label="公告类型" prop="noticeType">
              <el-select v-model="form.level" placeholder="请选择">
                <el-option
                  v-for="dict in typeOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col> -->
          <!-- <el-col :span="24">
            <el-form-item label="状态">
              <el-radio-group v-model="form.status">
                <el-radio
                  v-for="dict in statusOptions"
                  :key="dict.dictValue"
                  :label="dict.dictValue"
                >{{dict.dictLabel}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col> -->
          <el-col :span="24">
            <el-form-item label="内容">
              <textarea cols="82" rows="10" v-model="form.info"></textarea>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer" style="padding-top:20px">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getList,update,create,deleteData } from "@/api/systemSettings/notice";
export default {
  name: "Notice",
  components: {
    
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 公告表格数据
      noticeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 类型数据字典
      statusOptions: [
          {
              dictLabel:'正常',
              dictValue:0
          },
          {
              dictLabel:'关闭',
              dictValue:1
          }
      ],
      multipleSelection:[],
      // 状态数据字典
      typeOptions: [
          {
              dictLabel:'通知',
              dictValue:0
          },
          {
              dictLabel:'公告',
              dictValue:1
          }
      ],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        info: undefined,
        createBy: undefined,
        status: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        title: [
          { required: true, message: "公告标题不能为空", trigger: "blur" }
        ],
        info: [
          { required: true, message: "公告类型不能为空", trigger: "blur" }
        ]
      }
    };
  },
  filters: {
    formatDate: function (value) {
        let date = new Date(parseInt(value) * 1000)
        let y = date.getFullYear();
        let m = date.getMonth() + 1;
        m = m < 10 ? ('0' + m) : m;
        let d = date.getDate();
        d = d < 10 ? ('0' + d) : d;
        let h = date.getHours();
        h = h < 10 ? ('0' + h) : h;
        let minute = date.getMinutes();
        let second = date.getSeconds();
        minute = minute < 10 ? ('0' + minute) : minute;
        second = second < 10 ? ('0' + second) : second;
        return  y + '-' + m + '-' + d + ' '+ h + ':' + minute + ':' + second
    }
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询公告列表 */
    getList() {
      console.log(this.queryParams.info,this.queryParams.status)
      getList({
          page:this.queryParams.pageNum,
          pageSize:this.queryParams.pageSize,
          title:this.queryParams.info
      }).then((res) => {
          console.log(res)
            if(res.code == 200){
                this.noticeList=res.data.results
            }           
        })
        .catch(() => {
            console.log('error submit!!');
            return false;
        });
    },
    // 公告状态字典翻译
    statusFormat(row, column) {
    //   return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 公告状态字典翻译
    typeFormat(row, column) {
        console.log(row)
    //   return this.selectDictLabel(this.typeOptions, row.noticeType);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        info: undefined,
        title: undefined
      };
    //   this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(val) {
        this.multipleSelection = val; 
        console.log(val)
        if(this.multipleSelection.length>0){
          this.multiple=false
        }        
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加公告";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
        this.form= row;
        this.open = true;
        this.title = "修改公告";
    },
    /** 提交按钮 */
    submitForm: function() {
          if (this.form.id != undefined) {
            update(this.form).then(res => {
              if (res.code === 200) {
                this.$message.success("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            create(this.form).then(res => {
              if (res.code === 200) {
                this.$message.success("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
    },
    /** 删除按钮操作 */
    delAllSelection() {
        const length = this.multipleSelection.length;
        let str = '';
        // this.delList = this.delList.concat(this.multipleSelection);
        for (let i = 0; i < length; i++) {
            str += this.multipleSelection[i].id + ',';
        }
        if(str.length == 0){
            this.$message.error('必须选择一项！');
            return
        }
        this.$confirm('确定要删除吗？', '提示', {
            type: 'warning'
        })
            .then(() => {
                this.deleteData(str);
                this.multipleSelection = [];
            })
            .catch(() => {});
    },
    handleDelete(row) {
        this.$confirm('确定要删除吗？', '提示', {
            type: 'warning'
        })
            .then(() => {
                this.deleteData(row.id)           
            })
            .catch(() => {});
    },
    deleteData(id){
        deleteData({
                    'ids': id
                })
            .then((res) => {
                if(res.code == "200"){
                    this.$message.success('删除成功');
                    this.getList();
                }else{
                    this.$message.error('删除失败');
                }           
        })
        .catch(() => {
            console.log('error submit!!');
            return false;
        });
        },
  }
};
</script>
<style lang="scss" scoped>
textarea{
    // width: 95%;
    box-sizing: border-box;
    padding: 5px;
    color: #666;
    resize:vertical;
}
</style>